package org.FaPe3D.engine.render
{
	import flash.events.Event;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import org.FaPe3D.Adapter;
	import org.FaPe3D.engine.quality.Quality;

	public class FPRender extends FPBasicRenderEngine
	{
		private static var _renderInterval:Number;
		private static var _mc:*;
		private static var _mc2:*;
		private static var _super:FPBasicRenderEngine;
		
		public function FPRender()
		{
		}
		
		public static function render(seconds:Number = -1):void
		{
			_render(seconds);
		}
		
		public static function defaultRender(start:Boolean = true):void
		{
			if((_mc2 == null || _mc2 == undefined) && Adapter.currentAddapterID != null)
			{
				_mc2 = Adapter.getCurrentAdapter().viewports.getDefaultViewport().stage;
			}
			
			if(_mc2 != null && _mc2 != undefined)
			{
				if(start)
				{
					_mc2.addEventListener(Event.ENTER_FRAME, _basicrender);
				}else
				{
					_mc.removeEventListener(Event.ENTER_FRAME, _basicrender);
				}
			}
		}
		
		private static function _basicrender(e:Event)
		{
			_super.defaultRender();
		}
		
		private static function _render(seconds:Number = -1):void
		{
			clearTimeout(_renderInterval);
			if(Adapter.currentAddapterID != null && _mc == null)
			{
				_super = new FPBasicRenderEngine();
				_mc = Adapter.getCurrentAdapter().viewports.getDefaultViewport().stage;
			}
			//
			if(_mc != undefined)
			{
				if(_mc.hasEventListener(Event.ENTER_FRAME))
				{
					_mc.removeEventListener(Event.ENTER_FRAME, runRender);
				}
				
				if(seconds > 0)
				{
					// add event listeners
					_mc.addEventListener(Event.ENTER_FRAME, runRender);
				//	mc.stage.quality = StageQuality.LOW;
					//
					_renderInterval = setTimeout(function()
					{
				//		_container.stage.quality = StageQuality.HIGH;
						_mc.removeEventListener(Event.ENTER_FRAME, runRender);
						Quality.setQuality();
					}, seconds * 1000);
				}
			}
		}
		
		private static function runRender(e:Event):void
		{
			_super.defaultRender();
		}
	}
}